Recently, I had the opportunity to play around with some Docker containers and I must say that it is a quite useful tool.
In this post, I am going to summarize the main tasks that I usually perform with the docker engine but before we proceed, I wanted to point out the two aspects I like the most about it:
- Allows a workflow in which development folks build containers and operations folks runs them
- Containers offer a stronger isolation level than regular OS processes
Searching for an image
docker search "<SEARCH_TERMS>"
- This will search the docker hub for images relevant to the search terms.
Creating a container in attached mode
docker run -i -t <IMAGE_NAME> <SHELL_COMMAND>
SHELL_COMMANDis usually an actual shell environment such as
Creating a container in daemon mode
docker run -name <CONTAINER_NAME> -d -p <HOST_PORT>:<CONTAINER_PORT> <IMAGE_NAME> <COMMAND>
- It can be convenient to set a
CONTAINER_NAMEto refer to it easily.
-pallows to map a host tcp/udp port to the containers port.
COMMANDusually starts a server for example:
docker ps -a
-aallows to list stopped containers along with running containers
starting and stopping containers
docker start <CONTAINER_NAME | CONTAINER_HASH_ID>
- starts the container identified by its name or its hash id
docker stop <CONTAINER_NAME | CONTAINER_HASH_ID>
- stops the specified container
attaching the console to a running container
docker attach <CONTAINER_NAME | CONTAINER_HASH_ID>
- attach the current console to the specified container
docker exec <CONTAINER_NAME | CONTAINER_HASH_ID> <COMMAND>
COMMANDinside the specified container, this can be useful if attaching to the container will bring a console with the output of the main service running in the container
detaching the console from a running container without stopping it
Ctrl+P+Qwhen in a console attached to a container via
removing and renaming
docker rm <CONTAINER_NAME | CONTAINER_HASH_ID>to remove a container
docker rename <CONTAINER_NAME | CONTAINER_HASH_ID> <NEW_NAME>to rename a container