Docker cheat sheet

Posted by : on

Category : Infrastructure


Why you may want to read this article

When I’m doing some infrastructure stuff I realized that I did it in the past about 1 year ago but now I don’t remember anything. Then I end up googling it from scratch which is irritating, especially when you recall exactly this article that you cannot find for some reason now.

This article contains different useful docker-related guides, commands, and tutorials along with links that will include all the to-do steps for implementing various use cases.


Docker and Gitlab

Locally build image, push/pull to Gitlab registry with authorization

Actual on 2023-06-15.

Real-life example, I have node js application, and I would like to build it locally using docker and push it to the registry so my team can pull and run it. Or I can do that without building.

For that you need:

1. Authenticate yourself in Gitlab registry

Docs

I used Personal access token approach (PAT).

To generate Personal Access Token:

  • go to your profile: click on avatar on avatar -> Edit Profile
  • go to Access Tokens
  • create new one, personally I specified all the scopes, but I guess you need only write_registry and read_registry

On local machine login using docker cmd

docker login -u <your-user-name-or-email> -p <personal-access-token> registry.gitlab.com/<your-group-name>/<your-project-name>

<your-user-name-or-email> - the login name that you are using when log in to Gitlab

<personal-access-token> - the PAT that you have generated above

<your-group-name> - the group when project is located, if you go from UI to container registry you can find it in the url https://gitlab.com/your-group-name/your-project-name/container_registry

<your-project-name> - the project name, if you go from UI to container registry you can find it in the url https://gitlab.com/your-group-name/your-project-name/container_registry


2. Build image, from the folder where Dockerfile is located

docker build -t registry.gitlab.com/<your-group-name>/<your-project-name>/<image-name> .

<image-name> - the image name that you would like to see in Gitlab UI.


3. Push image

docker push registry.gitlab.com/<your-group-name>/<your-project-name>/<image-name>

After this step you can observer image is added to your registry in Gitlab UI.

alt_text

4. Pull image

Before pullilng images make sure your perform 1st step

docker pull registry.gitlab.com/<your-group-name>/<your-project-name>/<image-name>

5. Run pulled image

docker run registry.gitlab.com/<your-group-name>/<your-project-name>/<image-name>

About Andrii Bui

Hi, my name is Andrii. I'm Software Engineer at Microsoft with 5 years of experience.