Build and tag a docker image
Build and tag a docker image.¶
This guide assumes you are working with a Linux / MacOs device.
In case you are working locally on a Windows Os device You may be required for some adaptation.
According to your OS, install latest stable version of docker
Verify it is running correctly by following the Docker installation instructions.
Creating a docker file¶
Now that we have docker installed, let's create a
Create a file named
FROM nginx:1.17.8-alpine EXPOSE 80 CMD ["nginx", "-g", "daemon off;"]
This is a very simple
Dockerfile which does the following:
FROM: Downloads nginx image from docker hub public registry, with version(tag) 1.17.8-alpine.
EXPOSE: Informs Docker that the container listens on the specified network ports at runtime.
CMD: Which command gets executed when running the container.
Build a Docker image¶
Now we can build the image using this
docker build -t nginx .
The output should be similar to the below:
Sending build context to Docker daemon 2.048kB Step 1/3 : FROM nginx:1.17.8-alpine ---> 48c8a7c47625 Step 2/3 : EXPOSE 80 ---> Running in e1fa9dad51e0 Removing intermediate container e1fa9dad51e0 ---> a5c79fafafc8 Step 3/3 : CMD ["nginx", "-g", "daemon off;"] ---> Running in 30ce04b493ba Removing intermediate container 30ce04b493ba ---> dec62328d101 Successfully built dec62328d101 Successfully tagged nginx:latest
The image was built and then tagged with:
- This is docker default behavior when no tag is specified.
This image can only be used locally for your testing
but no one else can use it right now, since the image only exists locally and was not pushed to any registry.
You can see all images on your local environment by running:
docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest dec62328d101 4 minutes ago 21.8MB
Running The container¶
Run a container with the image you have just created
docker run --name nginx -p 80:80 nginx
We ask docker to:
- Start a container.
- We name that container
- We proxy requests from our network interface at port
80to the container docker interface in port
- The container should use the image
In your browser go to http://localhost
You should see nginx welcome page:
Go back to your CLI and you should see your browser request in the container STDOUT output.
Check which containers are running
find all created containers (Exited containers too)
docker ps -a
tail the container logs (starting from the last 20 rows)
docker logs -f nginx --tail 20
Stop and remove the container
To stop the container hit
The container will stop, but it is still exists.
You can start it again, with the same options it was created with, by running:
docker start nginx
To delete the stopped container run:
docker rm nginx
To remove a running container, you should stop it first:
docker stop nginx
Or force-delete of a running container
docker rm -f nginx
To run a container in the background run:
docker run -d ...
so far we built the container image locally,
however we can change that.
We could for example, build and tag the image with a specific registry (GitLab) and image tag So we could later push the image to our gitlab project registry path.
This will allow anyone with
registry_read permissions in our project to pull the image.
Create API TOKEN¶
First create an API token from your gitlab account:
- Click on your profile icon (top right corner) → settings → Access Tokens
- Name the token, select the api permissions, click on create personal access token
- Copy the generated token to your clipboard.
Login to gitlab registry¶
From your CLI run:
docker login registry.gitlab.com
The User is your Email address.
The Password is the Access Token you generated.
Create your gitlab project¶
Go to GitLab and create a new project:
- Make sure you create the repository under your gitlab group path and not under your username.
- Clone the new repository to your IDE as normally would, so that you will be able to commit and push files.
Project --> Clone (USE HTTPS)
You can use the same token you created earlier
or create another one for your IDE.
Pycharm is a great IDE for docker based projects development.
Pycharm also has docker plugin built in:
you can build, pass arguments and set options directly from the Interface.
Now you can build and tag the image with the registry address and the path of your project.
(We could tag the image with the gitlab registry address before, but we would not be able to
docker push the image without permissions.)
Always Build the image and Run the container on the same OS Platform.
Your registry address for the project would be:
If we created a project named
nginx under the
example group at gitlab We can tag the image like so:
docker build -t registry.gitlab.com/example/nginx .
Which will result an image with tag:
latest tag is used automatically when tagging docker images. do not specify it
Push your new image to the registry
docker push registry.gitlab.com/YOUR_GROUP_NAME_HERE/YOUR_PROJECT_NAME_HERE:v0.0.1
Now anyone with permission can
docker pull or
docker run this image and use it.
To pull the image from the registry without running it:
docker pull registry.gitlab.com/YOUR_GROUP_NAME_HERE/YOUR_PROJECT_NAME_HERE:0.0.1
This will pull
If we go back to our docker run command, now instead of using the official nginx registry We can create a container which will run our image:
docker run --name nginx -p 80:80 registry.gitlab.com/YOUR_GROUP_NAME_HERE/YOUR_PROJECT_NAME_HERE:0.0.1
If you want this version to also become the default(
latest) registry image,
You can tag it again, this time without a tag.
docker tag registry.gitlab.com/YOUR_GROUP_NAME_HERE/YOUR_PROJECT_NAME_HERE:0.0.1 registry.gitlab.com/YOUR_GROUP_NAME_HERE/YOUR_PROJECT_NAME_HERE
Then push the image (this will take just a few seconds, since the image already exists at the registry.)
docker push registry.gitlab.com/YOUR_GROUP_NAME_HERE/YOUR_PROJECT_NAME_HERE
Now you can get the
latest image by pulling without tag.
docker pull registry.gitlab.com/YOUR_GROUP_NAME_HERE/YOUR_PROJECT_NAME_HERE
This will pull the
latest tag automatically.
By now you have learned how to build, tag, push & pull docker images.
You also learned how to run, stop and remove docker containers, and how to view their logs.
Now, instead of doing this manually, let's move on to the next page and see What is GitLab and how we can docker build, tag & push images to our registry, automatically with GitLab Pipeline.