A Practicals Guide For Beginners To Kick Start Docker: Part 2

Ashutosh Singh
FAUN — Developer Community 🐾
4 min readAug 21, 2020

--

This is the second part of my docker tutorial article for the absolute beginner. You can read the first part here.

In this part of the article, I am talking about Docker Images and Docker containers.

Docker Images

A Docker image is an immutable (unchangeable) file that contains the source code, libraries, dependencies, tools, and other files needed for an application to run.

Since image it read-only quality, these images are sometimes referred to as snapshots.

It’s just a template you cannot start or run them.

Docker Containers

A Docker container is a virtualized run-time environment where users can isolate applications from the underlying system.

Let’s start with hands-on practical with docker images and docker containers.

Pulling images from docker hub-

Run the following command -

docker pull [image-name]

Retrieve all images from the system -

Run the following command-

docker images
or
docker image ls

Here You see, httpd image download successfully.

Running Images-

Run the following command-

docker run [image-name]

Let’s run the httpd image by using the following command:

docker run -dit -p 8082:80 httpd

In this example we’re using the following options in addition:

  • -dit: executes the container in interactive mode (but in the background).
  • -p 8082:80: by using the -p option we're connecting the internal port 80 of the container the external port 8082. Because the httpd server by default is running on port 80 we're then able to send the HTTP request to the server from our local machine by opening up URL http://localhost:8082.

To check all running container -

Run the following command -

docker ps

Giving container names

You can now give memorable names to your containers using the name flag for docker run. If no name is specified Docker will automatically generate a name.

docker run --name [give name] [image-nmae]r

Here you see I give my httpd container name is web-server.

View logs for a container

The docker logs command shows information logged by a running container.

docker logs [option] [container-id] or [container-name]

In this example we’re using the -f options in addition:

-f:Follow log output

Run any command in a running container-

docker exec command that can be used to connect to a container that is already running. Use docker ps to get the name of the existing container.

docker exec [option] [contaier-id] or [container-name] 

In this example we’re using the -it options in addition:

  • -i: interactive mode
  • -t: tty for getting terminal

Exit docker container without stopping it-

detach the container by pressing ctrl+p+q

Stop and start the docker container

You can stop and start one or more (all) containers at once.

docker start [OPTIONS] CONTAINER [CONTAINER...]docker stop [OPTIONS] CONTAINER [CONTAINER...]

Removing docker containers-

You can not remove running docker containers .if you try to remove running docker containers it gives a warning.

For removing containers you have to stop the docker containers.

Subscribe to FAUN topics and get your weekly curated email of the must-read tech stories, news, and tutorials 🗞️

Follow us on Twitter 🐦 and Facebook 👥 and Instagram 📷 and join our Facebook and Linkedin Groups 💬

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author! ⬇

--

--