Docker is one platform that allows running containers. The containers are processes where in this process have application and libraries necessary to run application.
Someone one moment said “to work in my machine” this problem occurs because when configuring applications in staging or production environments can have different versions of OS, libs and softwares used in the application. Docker allows specific version OS, libs and softwares prevent this problem mentioned above.
The virtualization will be installed on another OS over the machine, another side the docker is different from the virtualization where the docker uses the kernel of linux to run the processes docker called containers. The containers only necessary to run applications make containers light compared to virtualization.
To install docker use this link: https://docs.docker.com/get-docker/ in this link have instructions to install in windows, mac and linux. After install, back the article. Let’s begin.
Check docker installed:
Run docker container com nginx:
docker run nginx
Explain command above:
The command docker run é usando para criar um container docker
The name nginx é uma image docker. The image docker is one file where have instructions to configure nginx and your libraries.
Run docker container com nginx:
docker run --name=value_name nginx
The parameter --name=value_name is used for the set name the docker container.
Run docker container nginx:
docker run --name=value_name -p 8080:80 nginx
The parameter -p 8080:80 sets the docker containers allowing that when access port 8080 the host machine redirects for port 80 in docker container.
Run docker container background:
docker run -d -p 8080:80 nginx
The parameter -d in the command above is one indicator to run the container in background. Allowing the same terminal execute another action.
Stop container docker is running:
docker stop id_name_container
List containers docker running in moment:
List containers stopped:
docker ps -a
Remove container docker stopped:
docker rm id_or_name_container
Remove container docker is running:
docker rm -f id_or_name_container
Access and interact with container docker is running:
docker exec -it id_or_name_container command_execute_container
Explain command above:
The parameter -it will indicate you want interact with container docker
In place command_execute_container you can use the /bin/bash open terminal to interact with the container docker.
Using environment variable in docker container:
docker run -e KEY_NAME=VALUE image
The parameter -e after KEY_NAME=VALUE the flag -e is used when a set environment variable is needed for the docker container.
Using volumes in docker container:
docker run -d -v path_local_machine:path_in_container -e KEY_NAME=VALUE image
The volume is one functionality that allows persistent data in applications running in docker. Example: you execute command for running your application and the application write logs in file, case occur docker container died the log file is deleted, but you need maintain this log file same docker container died in this case volume is the solution, because when you execute a command to create docker container your application you set the volume this allow that log file write in path mapped in container is write in local machine, case docker container died file keep persisted in local machine. When docker container restarts, it gets a log file in the local machine and synchronizes the docker container.
List images docker in machine:
Download image docker the docker hub:
docker pull name_image
The docker hub is repository images docker is equal to github, but the docker hub focus store image docker.
Remove image docker the local machine:
docker image rm image_name
When you work in one application you have specific needs and it’s make needs you create your own Dockerfile. If you don't know what is docker until moment, calm down, I'll explain better about it.
Image Dockerfile with cake recipe where I have necessary instructions to make cake, in this case cake is container docker.
I’m imagining the now concept about dockerfile is ok. Let’s go example:
FROM node:15.13.0-alpine3.10 ENV PORT 4000 WORKDIR app/ COPY . ./ RUN npm i EXPOSE $PORT CMD node server.js
- Dockerfile is text file
- The parameter FROM is the start point in Dockerfile. This case I’m using node image with base. Case we want create one image absolute zero, replace node use a image scratch The parameter ENV is a one form set environment variable. In this case I’m creating one environment PORT with default value 4000.
- The parameter WORKDIR is used to set a directory to work. In this case set directory app/
- The parameter COPY is used as a copy file in the local machine for docker containers. In this case I copy all files local machine for docker container in directory app/
- The parameter RUN is used to execute commands with if typing command command line in terminal. In this case, I executed npm i to install dependencies on the project.
- The parameter EXPOSE is used more with documentation, because no expose this port. In this case the parameter is to indicate that necessary exposure port 3000.
- The parameter CMD is used to execute when docker container created
Create one image based Dockerfile:
docker build -f ./Dockerfile -t name_image:version .
Explain command above:
The parameter -f is used to set a path to your Dockerfile.
The parameter -t is used to set name, image and version.
The ‘.’ on the final command used for the specified current directory.
Send image for docker hub:
docker login --username=username_docker_hub
docker tag id_image_docker username/project_name:version
docker push username/project_name:version
Explain commands above:
The first command is used in docker hub(image repository).
The second command is used to create one tag for one image.
The third command is used to send images for docker hub.
Link the project in github: https://github.com/tiago123456789/project-for-article-docker
Docker composer is one tool that allows management of docker containers the simple way and prevents execute a lot of tasks in hand.
Before keeping reading this article using this link https://docs.docker.com/compose/install/ para install docker compose, after installed docker compose is needed create file called docker-composer.yaml where will be add instructions to run containers. The file docker-compose.yaml is yaml one file text what to work using indentation.
Below is one example of docker-compose.yaml file:
version: "3" services: app: build: ./ container_name: app_example environment: - PORT=3000 ports: - 3000:3000
- Line 1: is set version
- Line 3: from this line below is the service section where the docker containers that docker compose will be management.
- Line 5: set name the service.
- Line 6: using word build and set ./ this is one indicator mentioned docker compose to using Dockerfile with image this service.
- Line 7: set name the container
- Line 8 and 9: set environment variable used in application running into container.
- Line 10 and 11: set port your local machine and port application running into the container. When made a request in the local machine will redirect to a container running in this port.
Now, after define docker-compose.yaml is need run containers.
Running docker containers using docker-compose:
Running docker containers in background using docker-compose:
docker-compose up -d
Remove docker containers created using docker-compose:
I think that to the initial article about docker, it’s ok. Case you want to learn more about docker, comment below.