Docker - Container and Image
Docker Container
Docker container is a running instance
of an image
. You can use Command Line Interface (CLI)
commands to run
, start
, stop
, move
, or delete a container
. You can also provide configuration for the network
and environment variables
. Docker container is an isolated and secure application platform, but it can share and access to resources running in a different host
or container
.
Docker Image
An image is a read-only template with instructions
for creating a Docker container. A docker image is described in text file called a Dockerfile
, which has a simple, well-defined syntax
.
Example
You can understand container
and image
with the help of the following command:
$ docker run hello-world
The above command docker run hello-world
has three
parts:
- docker: It is
docker engine
and used to rundocker program
. It tells to the operating system that you are runningdocker program
. - run: This subcommand is used to create and run a
docker container
. - hello-world: It is a
name of an image
. You need to specify the name of an image which is to load into thecontainer
.
Special commands to use daily
Pulling Image:
$ docker pull nginx
- Used to pull an
image
from thedocker hub
andnginx
is the name of theimage
.
- Used to pull an
Showing Image:
$ docker images ឬ $ docker image ls
- Used to show all
images
contained in the local machine.
- Used to show all
Running Container:
$ docker run nginx:latest
- Used to run
container
, wherenginx
is thename of the image
, and thelatest
is theversion
of the image and must be associated with acolon(:)
in front of the version.
$ docker run -d nginx:latest
- Used to run the
container
same asdocker run nginx:latest
, but this command is executed indetached mode
, that is, run thecontainer
and you can use theconsole
where you arerunning the container normally
, but if you use the above command, you cannot use theconsole
.
- Used to run
Showing Container:
$ docker container ls ឬ $ docker ps
- Used to show all
containers
that are running.
$ docker ps -a
- Used to show all
containers
whether acontainer
isrunning
orstopped
.
$ docker ps -q
- Used to show only the
ID
of thecontainer
that is running.
$ docker ps -aq ឬ $ docker ps -a -q
- Used to show only the
ID
of thecontainer
, for bothrunning
andstopped
.
$ docker ps --format="ID\t{{.ID}}\nNAME\t{{.Names}}\nImage\t{{.Image}}\nPORTS\t{{.Ports}}\nCOMMAND\t{{.Command}}\nCREATED\t{{.CreatedAt}}\nSTATUS\t{{.Status}}\n"
- Used to show all
containers
that are running ineasy-to-view
format.
export FORMAT="ID\t{{.ID}}\nNAME\t{{.Names}}\nImage\t{{.Image}}\nPORTS\t{{.Ports}}\nCOMMAND\t{{.Command}}\nCREATED\t{{.CreatedAt}}\nSTATUS\t{{.Status}}\n"
- Used for
export path variables
in the machine for easy retrieval next time without having to write it more.
$ docker ps --format=$FORMAT
- Used to show a
container
that isformatted
by calling thepath variable
.
- Used to show all
Stop Container:
$ docker stop 19411e21c4fa
- Used to stop a
container
and19411e21c4fa
is thecontainer ID
.
$ docker stop boring_merkle
- Used to stop the
container
same as in the above command, butdocker stop 19411e21c4fa
above use thecontainer ID
, but this command uses thename of the container
, however you can use any options.
- Used to stop a
Exposing Port:
$ docker run -d -p 9000:80 nginx:latest
- Used to run the
container
indetached mode
, and it includes with explicit specification with theexpose port
, which is used to run thenginx
program, and to specify theexpose port
, and you need to use the-p
flag, and then the port you want to place, and then thecolon(:)
and then theport
of thecontainer itself
.
$ docker run -d -p 9000:80 -p 9010:80 nginx:latest
- Used to run the
container
same as in the above command, but for this command you can usemultiple ports
, ieport 9000
is successful orport 9010
is also successful.
- Used to run the
Start Container:
$ docker start 19411e21c4fa ឬ $ docker start boring_merkle
- Used to start an
existing container
and you want to use the option start with thecontainer ID
or use the start option with thecontainer name
.
- Used to start an
Removing Container:
$ docker rm 19411e21c4fa ឬ $ docker rm boring_merkle
- Used to remove the
container
, and this can be removed using thecontainer ID
or thecontainer name
, and you can remove the container only if the container isstopped
.
$ docker rm -f 19411e21c4fa ឬ $ docker rm -f boring_merkle
- Used to remove the
container
same as in the above command, but this is inforce mode
, which means removing the container without having to stop the container, and you can remove it using thecontainer ID
orcontainer name
.
$ docker rm $(docker ps -aq)
- Used to remove
all containers
, using thecontainer ID
forall stopped containers
.
$ docker rm -f $(docker ps -aq)
- Used to remove all
containers
, but you can delete both containers thatrunning and not running
.
- Used to remove the
Naming Container:
$ docker run -d --name nginx-ms -p 9000:80 nginx:latest
- Used to assign a name to what you want to name using
--name
flag, whilenginx-ms
is thecontainer name
.
- Used to assign a name to what you want to name using
Volumes:
$ docker run --name cambodia-website -d -p 9000:80 -v /home/mengsreang/Desktop/cambodia-website:/usr/share/nginx/html nginx:latest
- Used to run a container with specific volumnes that connect the project contained in your computer to the nginx server in the docker. A little simplification: The
-v
flag represents volumnes, while/home/mengsreang/Desktop/cambodia-website
is your project location on the computer, and/usr/share/nginx/html
is the project location of the nginx server. And don't forget to put acolon(:)
in front of it.
$ docker exec -it cambodia-website bash
- Used to create a bash to access a project on
nginx server
. Thatexec
command represents execute, while the-it
flag means execute bash ininteractive mode
.
- Used to run a container with specific volumnes that connect the project contained in your computer to the nginx server in the docker. A little simplification: The