Let’s start a Docker tutorial series by the most common commands you will ever type while working on your Dev-ops tasks.
I assume that you’ve at least followed the Docker Guide onto docs.docker.com
Here you will find some commands to manipulate your images, containers, services, volumes and networks. And for more details or deep learning, other guides will follow.
There are also various examples related to docker-compose which will be covered next.
I guess you already know about it but let’s review the commands to list containers and services :
docker ps -aq
The a parameter allows us to see all containers including the closed ones and the q parameter shows only the ids of the containers.
And for services :
docker service ls
docker service SERVICE_ID ps
The last command shows the service id with more details (see the network for instance).
Let’s see 1 command with several parameters :
docker run --rm -it --entrypoint /bin/sh IMAGE_NAME
The -it parameter allows us to interact with the container via a tty. You may also specify where you need your container to drop its messages (stdout, stderror using the -a parameter – see the documentation using docker run –help).
The –rm flag cleans up the image after stop.
The entrypoint flag allows us to override the default command of the image, here we want to access the shell commands instead of bash.
Checkout the next docker command :
docker run --rm -it --network host --mount type=bind,source=/home/user/Documents/config.yml,target=/etc/prog/config.yml -p 8080:80 image_name:version
Several things here :
- Passing the network flag and typing host makes our container reachable on our own local network.
- Next flag passes the source config file of our program to the container on start.
- The -p flag defines the external port on which we can access our container.
Networking & docker-compose
Make a service reachable on a shared network (ex: a service under monitoring network) :
Then you can attach a container as follow :
docker run ... --network monitoring_default
More about docker-compose
To exclude a service :
docker-compose up --scale service_name=0 -d
Beware the -d flag which set the remaining services to run in the background.
Stop and restart container after config update (modification of compose file) ;
docker-compose stop SERVICE_NAME && docker-compose up -d SERVICE_NAME
Updating an image needs us to remove the old one :
docker pull new_image
docker-compose up -d --remove-orphans
Example of a service update :
docker service update SERVICE_NAME --image IMAGE_NAME_AND_VERSION --force
Simply restart the service :
docker service update SERVICE_NAME --force
Copying data to/from container
As simple as the scp command :
docker cp CONTAINER_ID:/etc/program /home/user/Documents/program
There are various way to stop, remove and clean with Docker. Here is a quick recap :
Stopping all running containers :
docker stop $(docker ps -aq)
As you can notice, you are using a command to list the container ids then you stop them.
Removing all containers
docker rm $(docker ps -aq)
Removing all images
docker rmi $(docker images -q)
Remember to read the documentation for more details (ie : https://docs.docker.com/engine/reference/commandline/rmi/)
For docker services :
docker service rm myservice
In order to clean the system, issue the following commands :
docker system prune
Feel free to browse the web page about pruning.
For all images at once :
docker image prune -a
And finally for volume and network :
docker volume prune
docker network prune
Working with volumes :
Using docker-compose.yml :
Under volumes section add a folder to match the one in the image container (./data on the local current folder -> /var/lib/mysql is the container path).
Network and aliases :
Adding an alias under a network name to be able to talk to the service :
We can use db to talk to whatever, however instead of using the legacy links parameter (https://docs.docker.com/network/links/) to let the services talk to each other, you need to work on the same network and external_links.
In case you need other containers from outside to access to them, you should look at
Starting from a Dockerfile instead of an image
Let’s say you made a Dockerfile and plan on using it as a container with other services, you can make a compose file and link it to your image as follow :
Troubleshooting / Logs
In case a service is exiting, you can retrieve the logs using :
docker logs CONTAINER_ID
For containers that do not start, remember :
docker ps -a
Then log the details using docker logs and the failing container id.
Using grep with docker logs :
docker logs CONTAINER_ID 2>&1 | grep "Whatever"
In this command, the 2 refers to stderr and the 1 to stdout. The >& appends everything from stderr to stdout. You may not need to use this parameter but if Docker is configured to log details toward the error console, then this trick will help.
Live logs using tail:
docker logs -f tail 200 CONTAINER_ID
The -f parameter keeps printing logs while the container is running. The 200 prints out 200 last lines.
Timing example :
docker logs -t --since 2019-12-13T08:30:00+02:00 --until 2019-12-13T09:30:00+02:00
Above & below lines :
docker logs CONTAINER_ID 2>&1 | grep "whatever" -B 10 -A 10
If you have trouble with an image, make sure you run the latest version, a hash comparison will provide you with a quick view :
docker image inspect imageID
Additional tutorials will cover the monitoring subject, here we introduce a simple command to have a quick look on a swarm environment :
Resource limiting for docker services
First of all inspect the details of the service :
docker service inspect YOUR_SERVICE
Limit the memory :
docker service update YOUR_SERVICE --limit-memory 2GB
Saving and Loading an image
If you don’t have access to a remote repository to save your images, you may us the following commands and send the files directly to production :
docker save -o image_file LATEST
docker load -i image_file
That’s it for today folks ! It’s a bit messy but can serve while looking at precise needs. It does for me actually and I plan on updating this page regularly with additional examples.
Next Docker tutorials will cover specific exercises to train yourself step by step to get a better grasp on the area.
Changing the config on a docker service
Whenever you have a service (could be under swarm…) running in production and you need to alter the configuration parameters (if there are not way to modify the settings on the fly with for instance Actuator), then you will need to restart the service and apply a new configuration.
Following are the steps :