Run Docker Container
For this You need basic knowledge of Docker like what is docker and why to use docker. so you can understand bit more.
Note: The commands used in this article will be the same regardless of if you are using a Windows, Linux, or Mac system.
Let’s run our first loved Docker Image Hello world
so, Here we go.
docker run <Image_name>
here I used hello-world image which print Hello from Docker , You might get some different output when you run this container based on this image. for first time because currently hello-world image is in my current machine or a Disk but when we try first time that time docker will not found hello-world in current machine or a Disk and it pull Image from Docker hub. After downloading image whenever you run that image then it use from our disk.
Docker Hub is a service provided by Docker for finding and sharing container images with your team.
Let’s understand whole flow,
- first we are giving command to create or run container based on image hello-world.
- Since the hello-world image wasn’t already present on your disk, Docker downloaded it from a default registry, the Docker Hub.
- Docker created a container based on the hello-world image.
- After that it put some text on console so we get that container is running.
- And last, The container stopped.
Docker container life cycle
here, first we can create new container from image and then start or run with docker run command and we can make container as long lived or stop after run (we will see long lived contained as well). In hello-world example it is stopped after executing it. After that delete that container.
whenever we run any container based on Image then it use our local’s or Docker hub’s image. Image is stored in registry.
Container Management Commands
- docker ps: lists the containers that are still running. Add the -a switch in order to see containers that have stopped. it give container id with details.
-dRun container in background and print container ID
docker ps or docker ps -a
- docker logs: retrieves the logs of a container, even when it has stopped
docker logs <container id>
- docker inspect: gets detailed information about a running or stopped container
docker inspect <container id>
- docker stop: stops a container that is still running
docker stop <container id>
- docker rm: deletes a container
docker rm <container id>
all commands has their options as well. we will see some them in next part.
here is simple run single command printenv of alpine(Image) based container.
Can we create more then one container from one image ? Yes we can,
docker container prune -f
With docker container prune -f command we can remove all stopped containers
Note : The container ID is quite long. However, you don’t need to write it entirely in your commands. As long as there is no ambiguity, you can use the beginning of the container ID in commands that require the container ID, like docker logs or docker run. Using the beginning of the container ID comes in handy when you’re managing containers manually.
Till now, We just saw how to run short-lived containers. They usually do some processing and display some output. However, there’s a very common use for long-lived containers: server containers. Whether you want to host a web application, an API, or a database, you want a container that listens for incoming network connections and is potentially long-lived. let see for Long-lived container now
Listening for Incoming Network Connections
let’s use NGINX web server. If I simply run the server, my machine does not route incoming requests to it unless I use the -p switch on the docker run command.
docker run -d -p 8085:80 nginx
here, The -p switch takes two parameters; the incoming port you want to open on the host machine, and the port to which it should be mapped inside the container. For instance, here is how I state that I want my machine to listen for incoming connections on port 8085 and route them to port 80 inside a container that runs NGINX.
You can see at localhost:8085 I am getting nginx’s default page. Welcome to nginx!
Container can be stateful or stateless. In stateful container can store some state in container and in stateless container can not store previous states. so, means we can not store data in container. reason for having stateless containers is that this allows for easy scaling up and recovery.
When a container writes files, it writes them inside of the container. Which means that when the container dies (the host machine restarts, the container is moved from one node to another in a cluster, it simply fails, etc.) all of that data is lost. It also means that if you run the same container several times in a load-balancing scenario, each container will have its own data, which may result in inconsistent user experience.
So suppose we are using database server in one container and after any kind of failure data also lose. for this we can use volumes with volumes we can map containers data with our local files or data. like,..
docker run -d mysql:5.7
It will run mysql 5.7 in docker container as a database. Any data stored in that database will be lost when the container is stopped or restarted. In order to avoid data loss, you can use a volume mount:
docker run -v /project/dir:/var/lib/mysql -d mysql:5.7
It will ensure that any data written to the /var/lib/mysql directory inside the container is actually written to the /project/dir directory on the host system. This ensures that the data is not lost when the container is restarted.
Did you notice above i write mysql:5.7 not only mysql. so here 5.7 is tag for mysql Image. Tag is use for versioning.
docker run <name>:<tag>
In this we learn how to run docker container and how it work. In next we will see how to create Docker Image with example.