How To Use Docker in Redhat 7/Centos 7

0
1423

Once we docker installed, its intuitive usage experience makes it very easy to work with. By now, once we started docker service now let we make sure docker service is running.

To check the docker daemon:

# systemctl status docker.service

Syntax:

Using docker consists of passing it a chain of options and commands followed by arguments. Please note that docker needs root privileges in order to work.

# docker [option] [command] [arguments]

Let’s check all available docker commends;

Ask docker for a list of all available commands:

# docker

All currently (as of 1.11.2) available commands:

attach   Attach to a running container

build     Build an image from a Dockerfile

commit   Create a new image from a container’s changes

cp       Copy files/folders between a container and the local filesystem

create   Create a new container

diff     Inspect changes on a container’s filesystem

events   Get real time events from the server

exec     Run a command in a running container

export   Export a container’s filesystem as a tar archive

history   Show the history of an image

images   List images

import   Import the contents from a tarball to create a filesystem image

info     Display system-wide information

inspect   Return low-level information on a container or image

kill     Kill a running container

load     Load an image from a tar archive or STDIN

login     Log in to a Docker registry

logout   Log out from a Docker registry

logs     Fetch the logs of a container

network   Manage Docker networks

pause     Pause all processes within a container

port     List port mappings or a specific mapping for the CONTAINER

ps       List containers

pull     Pull an image or a repository from a registry

push     Push an image or a repository to a registry

rename   Rename a container

restart   Restart a container

rm       Remove one or more containers

rmi      Remove one or more images

run       Run a command in a new container

save     Save one or more images to a tar archive

search   Search the Docker Hub for images

start     Start one or more stopped containers

stats     Display a live stream of container(s) resource usage statistics

stop     Stop a running container

tag       Tag an image into a repository

top       Display the running processes of a container

unpause   Unpause all processes within a container

update   Update configuration of one or more containers

version   Show the Docker version information

volume   Manage Docker volumes

wait     Block until a container stops, then print its exit code

To check more system information and version on docker

# docker info

# For docker version:

# docker version

Let’s start working on Images

As we have discussed at length, the key to start working with any docker container is using images. There are many freely available images shared across docker image index and the CLI allows simple access to query the image repository and to download new ones.

When you are ready, you can also share your image there as well. See the section on “push” further down for details.

We can search a docker images using below commend

Format:

# docker search [image name]

For example will search openSUSE linux

# docker search suse

This will provide you a very long list of all available images matching the query: SUSE.

Downloading an image:

Either when you are building / creating a container or before you do, you will need to have an image present at the host machine where the containers will exist. In order to download images (perhaps following “search”) you can execute pull to get one.

Syntex:

# docker pull [image name]

Let’s download the openSUSE image

# docker pull opensuse

docker pull

Listing images:

We can list all available images on docker,

# docker images

REPOSITORY         TAG                 IMAGE ID           CREATED             SIZE

opensuse           latest             642bc8a45961       3 days ago         97.74 MB

hello-world         latest             c54a2cc56cbb       2 weeks ago         1.848 kB

Sharing (PUSHing) images:

If you would like share image to all in the world, you can use push to have your image listed in the index where everybody can download and use.

Please remember to “commit” all your changes.

To commit,

# docker commit [container ID] image_name# docker commit 7dqd9w543d89 opensuse Syntax:

# docker push [username/image name]

# docker push my_username/my_first_image

You can to sign-up at index.docker.io to push images to docker index.

Working with Containers

When you “run” any process using an image, in return, you will have a container. When the process is not actively running, this container will be a non-running container. Nonetheless, all of them will reside on your system until you remove them via rm command.

Listing all current containers:

By default, you can use the following to list all running containers:

# docker ps

To have a list of both running and non-running ones, use:

# docker ps -l

Creating a New Container

It is currently not possible to create a container without running anything (i.e. commands). To create a new container, you need to use a base image and specify a command to run.

Syntex

# docker run [image name] [command to run]

# docker run opensuse echo “hello”

We can name a container instead of having long IDs

# docker run –name [name] [image name] [comm.]

# docker run –name open_cont 642bc8a45961 echo “hello”

This will output “hello” and you will be right back where you were.

As you cannot change the command you run after having created a container (hence specifying one during “creation”), it is common practice to use process managers and even custom launch scripts to be able to execute different commands.

To make sure container status, we can check through

# docker ps

Docker ps

Running a container:

When you create a container and it stops (either due to its process ending or you stopping it explicitly), you can use “run” to get the container working again with the same command used to create it.

To start a container:

# docker run [container ID]

Example:

# docker run c629b7d70666

Stopping a container:

To stop a container’s process from running:

# docker stop [container ID]

# docker stop c629b7d70666

To Start a container

# docker start [container ID]

# docker start ec6b27c52c88

Saving (committing) a container:

If you would like to save the progress and changes you made with a container, you can use “commit” as explained above to save it as an image.

This command turns your container to an image.

Remember that with docker, commits are cheap. Do not hesitate to use them to create images to save your progress with a
container or to roll back when you need (e.g. like snapshots in time).

Removing / Deleting a container:

Using the ID of a container, you can delete one with rm.

# Usage: sudo docker rm [container ID]

# docker rm c629b7d70666

NO COMMENTS