LinuxTechLab.com
Important Docker commands for Beginners
Originally published on
LinuxTechLab.com
In our earlier tutorial, we learned to install Docker on RHEL\ CentOS 7 & also created a docker container. In this tutorial, we will learn more commands to manipulate a docker container.
Syntax for using Docker command $ docker [option] [command] [arguments] To view the list of all available commands that can be used with docker, run $ docker & we will get the following list of commands as the output, attach build commit cp create diff events exec export history images import info inspect kill load login logout logs network pause port ps pull push rename restart rm rmi run
Attach to a running container Build an image from a Dockerfile Create a new image from a container’s changes Copy files/folders between a container and the local filesystem Create a new container Inspect changes on a container’s filesystem Get real time events from the server Run a command in a running container Export a container’s filesystem as a tar archive Show the history of an image List images Import the contents from a tarball to create a filesystem image Display system-wide information Return low-level information on a container or image Kill a running container Load an image from a tar archive or STDIN Log in to a Docker registry Log out from a Docker registry Fetch the logs of a container Manage Docker networks Pause all processes within a container List port mappings or a specific mapping for the CONTAINER List containers Pull an image or a repository from a registry Push an image or a repository to a registry Rename a container Restart a container Remove one or more containers Remove one or more images Run a command in a new container
save search start stats stop tag top unpause update version volume wait
Save one or more images to a tar archive Search the Docker Hub for images Start one or more stopped containers Display a live stream of container(s) resource usage statistics Stop a running container Tag an image into a repository Display the running processes of a container Unpause all processes within a container Update configuration of one or more containers Show the Docker version information Manage Docker volumes Block until a container stops, then print its exit code
To further view the options available with a command, run $ docker docker-subcommand info & we will get a list of options that we can use with the docker-sub command.
Testing connection with Docker Hub By default, all the images that are used are pulled from Docker Hub. We can upload or download an image for OS from Docker Hub. To make sure that we can do so, run $ docker run hello-world & the output should be, Hello from Docker. This message shows that your installation appears to be working correctly. ‌ This output message shows that we can access Docker Hub & can now download docker images from Docker Hub.
Searching an Image To search for an image for the container, run $ docker search Ubuntu & we should get list of available Ubuntu images. Remember if you are looking for an official image, look for [OK] under the official column.
Downloading an image Once we have searched and found the image we are looking for, we can download it by running, $ docker pull Ubuntu
Viewing downloaded images To view all the downloaded images, run $ docker images
Running an container To run a container using the downloaded image , we will use $ docker run –it Ubuntu Here, using ‘-it’ will open a shell that can be used to interact with the container. Once the container is up & running, we can then use it as a normal machine & execute any commands that we require for our container.
Displaying all docker containers To view the list of all docker containers, run $ docker ps The output will contain list ofcontainers with container id.
Stopping a docker container To stop a docker container, run $ docker stop container-id
Exit from the container To exit from the container, type $ exit
Saving the state of the container Once the container is running & we have changed some settings in the container, like for example installed apache server, we need to save the state of the container. Image created is saved on the local system. To commit & save the state of the container, run $ docker commit 85475ef774 repository/image_name Here, commit will save the container state, 85475ef774, is the container id of the container, repository, usually the docker hub username (or name of the repository added) image_name, will be the new name of the image. We can further add more information to the above command using ‘-m’ & ‘-a’. With ‘-m’, we can mention a message saying that apache server is installed & with ‘-a’ we can add author name.
For example docker commit -m “apache server installed”-a “Dan Daniels” 85475ef774 daniels_dan/Cent_container
This completes our tutorial on important commands used in Dockers, please share your comments/queries in the comment box below.
If you think we have helped you or just want to support us, please consider these :Connect to us: Facebook | Twitter | Google Plus
LinuxTechLab.com