Introduction
I was trying to remember by looking at the official Docker manual when I was operating various things.
I started to do various things and I couldn't remember it anymore.
I made a list of frequently used commands and wrote them out.
Container creation operation related
** I've listed some of the most commonly used ones below, but there are more run options, so **
** Please refer to Docker URL. ** **
- Option to restart automatically when the host is restarted or the container is stopped unintentionally
docker run --restart=always
- Mount the container volume on the host
docker run -v [Volume path on the host side]:[Path in the container] [The name of the container image you want to start]
- Access the container from the outside (port forwarding)
You can specify more than one
docker run -p [External port]:[Container port]
Login to container and command execution
- Login to the container
docker exec -it [Container ID or container name] /bin/bash
- Command execution in the container
docker exec [Container ID or container name] [Command you want to execute]
Container status check operation
- Display a list of currently running containers
Check if the STATUS column is UP to see if it is running
docker ps
- Display a list of stopped / running containers
- -A is an abbreviation for --all
docker ps -a
Check container log
- It's like displaying the container log with cat
docker logs [Container name or container ID]
- Add the time when outputting the log
docker logs -t [Container name or container ID]
- Continue to display logs in real time
docker logs -f [Container name or container ID]
- Display the specified number of lines from the end of the log
docker logs --tail=[Number of lines to display] [Container name or container ID]
Delete container operation
- Delete the stopped container
docker rm [Container name or container ID]
- Delete the running container
- It is originally desirable to delete after stopping.
docker rm -rf [Container name or container ID]
- Delete all stopped containers
Extract all the containers stopped with -a, extract the container ID from them with -q, and delete it.
- As for the running container, an error will occur and it cannot be deleted.
In that case, stop the running container before executing.
docker rm $(docker ps -q -a)
Container stop / start / restart operation
- Stop a specific container that is running
docker stop [Container name or container ID]
- Start a specific container that is stopped
docekr start [Container name or container ID]
- Restart a specific container
docekr restart [Container name or container ID]
- Stop all containers
Extract container ID with -q and stop container
docker stop $(docker ps -q)
- Start all containers
Extract the container ID with -q and start the container
docker start $(docker ps -q)
Docker operation Other
- Deletes all stopped containers, unused networks, unused images, etc.
However, please be careful when executing it as it will be deleted all at once. .. ..
docker system purne
- Display the detailed information of the created container or image name
docker inspect [Container name or container ID or image ID]
- Change the container name
docker rename [Container name before change] [The changed container name]
Finally
This time, I mainly touched on creating and deleting containers in Docker.
When I write next time, I would like to write something that goes a little further.