Docker Container is an environment that contains an application, or multiple applications, and all the libraries, other applications and tooling they need to run.

The application is encapsulated with its dependencies in a container. You can take a Docker Image and create a container from it on a newly bought machine with Docker installed, and your application will work without additional work or dependencies.

How to install Docker ?

The exact installation instructions depend on your operating system. The Docker Website contains detailed instructions for

Launch your first container
docker container run <options> <image>

Port mapping

When I run a web server like nginx for example, it will render web pages on port 80, but only inside the container. I wouldn’t have access to it because it’s totally isolated, the container has its own network. In order to be able to access the web pages, I will use the -p option which will allow me to specify the port of my machine and tell it to which port of the container I want to link. This way, I will be able to access the web pages through my browser.

This is an example that opens port 80 of the container to port 9001 of my machine.

docker container run -d -p 9001:80 nginx

And now if I go to the URL http: // localhost: 9001 / in my browser, I can see the nginx home page.

CONTRÔLE DES PORTS

With the following command, I can see a “PORTS” column that tells me which port on my machine is exposed to a container.

docker container ls

//or 

docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
828bbc8ba775        nginx               "nginx -g 'daemon of…"   2 minutes ago       Up 2 minutes        0.0.0.0:9001->80/

There is also a command to get port data for a specific container.

docker port <container_ID>

The notation is reversed (compared to an ls) . Example: 80/tcp -> 0.0.0.0:9001

What is Dockerfile ?

A Dockerfile is a text file with instructions on how to build an image.

Those instructions are part of a configuration language, which includes keywords like FROMLABELRUNCOPYENTRYPOINTCMDEXPOSEENV and more.

This is the workflow: first you create a Dockefile, then you built a Docker image from it using docker build, and finally you run a container from the image.

Example: Let’s create our first Dockerfile

Let’s say you have a folder with a simple Node.js app composed by an app.js, a package.json file that lists a couple dependencies you need to install before running the app, and package-lock.json.

Inside it, create a plain text file named Dockerfile, with no extension, with this content:

FROM node:14
WORKDIR /usr/src/app
COPY package*.json app.js ./
RUN npm install
EXPOSE 3000
CMD ["node", "app.js"]

n the first line we say which image we want to start from. This will be our base image. In this case it will take the official Node.js image, based on Alpine Linux, using Node 14. When creating a container from the Dockerfile, Docker will get that image from Docker Hub.

Next we set the working directory to /usr/src/app, which means all our commands will be run in that folder until we change it again. That’s a folder we know already exists in the Node image.

We copy the package.jsonpackage-lock.json (using the * wildcard) and app.js files that are present in the current folder, into the working directory.

We run npm install to install the packages listed in the package.json file.

Then we expose port 3000 to the outside, since that’s what our app runs on. A container is 100% isolated from the network unless you expose one of its ports using the EXPOSE command.

Finally we run node app.js to start the app

 How to create a container from a DockerFile ?

Open a terminal and go to the app directory with the Dockerfile. Now build the container image using the docker build command.

 docker build -t <my-customize-container-name> .

This command used the Dockerfile to build a new container image.

 the -t flag tags our image. Think of this simply as a human-readable name for the final image.

The . at the end of the docker build command tells that Docker should look for the Dockerfile in the current directory.

How to get Log-info about specific service in docker?
docker logs -f <service>
Restart a service in docker
docker restart <service>
Check Docker version
$ docker version 

It shows docker version for both client and server. As given in the following image.

Build Docker Image from a Dockerfile
$ docker build -t image-name docker-file-location  

-t : it is used to tag Docker image with the provided name.

Run Docker Image
$ docker run -d image-name  

-d : It is used to create a daemon process.

Check available Docker images
$ docker images  
Remove All Docker Images
docker rmi $(docker images -q)

How It Works ? : docker images -q will list all image IDs. We pass these IDs to docker rmi (which stands for remove images) and we therefore remove all the images.

Check for latest running container
$ docker ps -l  

-l : it is used to show latest available container.

Check all running containers
$ docker ps -a  

-a : It is used to show all available containers.

Stop running container
$ docker stop container_id  

container_id : It is an Id assigned by the Docker to the container.

Stop All Docker Containers
docker kill $(docker ps -q)
Remove All Docker Containers

If you don’t just want to stop containers and you’d like to go a step further and remove them, simply run the following command:

docker rm $(docker ps -a -q)

Delete an image
$ docker rmi image-name  
Delete all images
$ docker rmi -f $(docker images -q)  

f : It is used to delete image forcefully.

Delete all containers
$ docker rm $(docker ps -a -q)  
Enter into Docker container
$ docker exec -it container-id bash  
Reference:

https://flaviocopes.com/

https://nouslesdevs.com

By Shabazz

Software Engineer, MCSD, Web developer & Angular specialist

Leave a Reply

Your email address will not be published. Required fields are marked *