Skip to content
JackSparrow414
Go back

Using Docker (Part 2)

Table of contents

Open Table of contents

Article body

After installing Docker, I started using it. From what I had read online, the basic idea was this: previously, to use CentOS, I would install a virtual machine and configure its operating system. The drawback is that a VM consumes substantial resources on the host. I experienced this firsthand: after installing a VM on my Mac, the fans ran hard almost every day, and VirtualBox’s CPU usage shot up. With Docker, I can download a CentOS image of just a few hundred megabytes and get the functionality I previously used a VM for.

Step 1: obtain an image. There are essentially two approaches: pull an image from the registry provided by Docker, or write your own Dockerfile to customize it. I prefer the second approach. I will cover both in detail later.

Step 2: after pulling or building an image, use it to create a container. List existing images with ----docker image ls

Step 3: create a container with the command ----docker -it —name CONTAINER_NAME IMAGE_NAME:IMAGE_TAG  /bin/bash. This creates and starts a container. The it flags provide interactive access, and /bin/bash invokes Bash inside the container after startup. I recommend studying the individual container-creation options to deepen your understanding of Docker.

Example: docker run -it —name mydocker  myImage:latest /bin/bash

Step 4: if step 3 succeeded, your Mac terminal should now show a CentOS shell prompt, such as root@abcdefg.

You now have a virtual CentOS system to experiment with. Go ahead and play around.

Step 5: there are two ways to leave the container: 1. Enter exit to leave and stop it. 2. Press ctrl+P+Q to detach without stopping it. I prefer the second option.

Step 6: after leaving the container, I think it is useful to commit its changes to an image. If I need a similar container again, I can simply run another one from that image, without having to keep this container indefinitely. With a configured image, I can delete and recreate containers as needed. The command is ---docker commit -a “AUTHOR_NAME” -m “DESCRIPTION” -p (defaults to true; stops the container after the commit completes)  CONTAINER_NAME REPOSITORY_NAME:TAG (usually latest)

Example: docker commit -a “ly” -m “add change” mydocker  firstCommit:latest

Now —docker image ls will show the new image, firstCommit.

Step 7: so far, the commit has only created a local image. To publish it to your own online repository, run docker tag LOCAL_IMAGE_NAME DOCKER_USERNAME/REPOSITORY_NAME (the repository will be created by default if it does not exist).

Example: docker tag firstCommit ly/dockerRepository

Note: if the image you created in step 6 uses a tag other than latest, such as firstCommit:v1, include that tag after the image name here: docker tag firstCommit:v1  ly/dockerRepository

After running that command, push the local image to the remote repository with docker push USERNAME/REPOSITORY_NAME

Example: docker push ly/dockerRepository

Remember to log in to Docker. Otherwise, you may get “denied: requested access to the resource is denied.” If you are already logged in and still see this error, try using the standard tag format before pushing again.

Step 8: now that the image has been pushed to the remote repository, we can remove the local copy. Before deleting the image, remove any containers that depend on it. Stop the containers before removing them.

Example: docker stop mydocker   (stop the container)

           docker rm mydocker (remove the container)

          docker rmi IMAGE_ID (or IMAGE_NAME) (remove the image). Note: using the image ID is usually sufficient. Sometimes two images have the same image ID and dependencies, in which case you can remove one by name.

That completes a basic Docker workflow. There are many details I have not covered yet; I will go through them in later posts. The goal here was simply to get through the workflow successfully.

I have to admire the design: what once required a large virtual machine can now be handled by a single process. ??

Update: 2019-1-16

One addition: if neither of the image-removal approaches above works, try the following:

docker rmi IMAGE_NAME:TAG will remove it.

To publish an image, you can use docker tag IMAGE_NAME USERNAME/REPOSITORY_NAME:latest

Then run docker push IMAGE_NAME USERNAME/REPOSITORY_NAME


Share this post:

Continue this series

Installing and Using Docker

  1. Installing and Using Docker (Part 1)
  2. Using Docker (Part 2)You are here

Comments

Questions, corrections, and experiences are welcome. Sign in with GitHub to comment; both language versions share this discussion.

Comments are available on the live site only.