The following post shows the default workflow to run and update a Docker image based on the example of GitLab. But this should also work for other containers.
Docker Installation
First make sure that your local machine has Docker installed. I use Debian and for this, a detailed description is available in Dockers documentation. There are also instructions for CentOS, Ubuntu, Fedora and others.
You can check the installed Docker version:
docker version
Client: Docker Engine - Community
Version: 20.10.11
API version: 1.41
...
Run a Docker Container
Let’s start with an environment variable that defines the path where all the Gitlab data is stored. This is outside of the Docker container which allows an easy update without loosing data. To define the variable $GITLAB_HOME
, you can use:
export GITLAB_HOME="/srv/gitlab"
To run a Docker container, simply use the run
command with your parameters. For GitLab, this might look like:
docker run --detach
--hostname <host.example.com>
--publish 443:443 --publish 80:80 --publish 2222:22
--name gitlab
--restart always
--volume $GITLAB_HOME/config:/etc/gitlab
--volume $GITLAB_HOME/logs:/var/log/gitlab
--volume $GITLAB_HOME/data:/var/opt/gitlab
gitlab/gitlab-ce:latest
You should use your own settings and replace <host.example.com>
with your setup.
For the sake of completeness, you might also need a runner for GitLab. Let’s run
this container as well:
docker run --detach
--name gitlab_runner
--restart always
--network host
-v /run/docker.sock:/var/run/docker.sock
gitlab/gitlab-runner:latest
And this one needs to be registered in your GitLab instance:
docker exec -it gitlab_runner gitlab-runner register
--url https://<host.example.com>/
--registration-token <registration_token>
More details about the configuration of the GitLab runner is descriped in this post.
Update a Docker Container to the Latest Version
Let’s check, which container are running by using:
docker ps
The output shows all your containers and their ids:
CONTAINER ID IMAGE COMMAND CREATED STATUS NAMES
02fe426970f1 gitlab/gitlab-ce:latest "/assets/wrapper" 4 hours ago Up 4 hours (healthy) gitlab
5742e07f809b gitlab/gitlab-runner:latest "/usr/bin/dumb-init …" 4 days ago Up 5 hours gitlab_runner
We have to stop the container and remove the image. When you used a run command (for GitLab) as shown above, this should not affect the data as this is stored outside of the container.
docker stop 02fe426970f1
docker rm 02fe426970f1
To remove the associated image, you can use the docker rmi
. For this, we need to find the id of this image by listing all images:
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
gitlab/gitlab-ce latest 46cd6954564a 17 hours ago 2.36GB
mysql 5.7 c20987f18b13 2 weeks ago 448MB
cirrusci/flutter 2.8.1 98a87781f179 2 weeks ago 3.49GB
Now we can remove the image (in my case the gitlab-ce image):
docker rmi 46cd6954564a
The update of the image can be done by using pull
. This will download the images (in this case the latest version) and stores it on your system:
docker pull gitlab/gitlab-ce:latest
After this, you can use the same run command as before to run a container with the updated image:
docker run --detach
--hostname <host.example.com>
--publish 443:443 --publish 80:80 --publish 2222:22
--name gitlab
--restart always
--volume $GITLAB_HOME/config:/etc/gitlab
--volume $GITLAB_HOME/logs:/var/log/gitlab
--volume $GITLAB_HOME/data:/var/opt/gitlab
gitlab/gitlab-ce:latest
A final check, shows the container is up and running:
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS NAMES
f3dab1163c07 gitlab/gitlab-ce:latest "/assets/wrapper" 2 minutes ago Up 4 hours (healthy) gitlab
5742e07f809b gitlab/gitlab-runner:latest "/usr/bin/dumb-init …" 4 days ago Up 5 hours gitlab_runner
Instead of automatically using the latest version of each image (e.g. gitlab/gitlab-ce:latest
) you can also specify a version number like gitlab/gitlab-ce:14.5.2
. This is helpful when you need to update a system in steps. Or if you need to follow a specific upgrade path, which is the case for GitLab.
Photo by Ian Taylor on Unsplash.
Leave a Reply