Containers

Container Basics: How to Deploy a Container with Docker

14 May 2019 9:05am, by

Imagine, in order to roll out a website, that you don’t have to install a full-blown server from bare metal up. Instead of installing the operating system, followed by the server software, followed by the deployment of your perfectly-crafted app or site, you could simply develop everything in a self-contained bundle and roll it out with a single command.

That’s one of the many benefits of using containers. They make the dev and deploy cycle incredible efficient. But how do you deploy those containers? I want to walk you through that process here. We’ll focus on deploying a basic NGINX web server, as a container, on Ubuntu Server 18.04. All of this will be done with the help of Docker.

What You’ll Need

In order to successfully deploy NGINX as a container, you’ll need to have the following:

That’s it. With those two pieces in hand, you’re ready to go.

A Gentle Reminder

Every container you deploy will be based on an image pulled from DockerHub. You can pull down a single image and use it as often as you like. There are also numerous images on DockerHub for a single application or platform. Take, for instance, NGINX. If you search for NGINX on DockerHub, you’ll come up with around 56,172 entries. That doesn’t mean each of those entries is a viable image for you to use.

Images are named like so:

For example, with NGINX, you might find an image named:

Or you might find:

So you see, there are images for just about every need.

At this time, there is only one image you want to focus on. That image is the official release from NGINX.

Pulling the Image

Before you can use an image, it has to be saved on your local drive. There are two ways to do this:

  • Directly, with the pull command.
  • Indirectly, during the container deployment process.

To pull an image from DockerHub, you would open a terminal window and issue the command:

If you wanted to pull the Ingress image, that command would be:

Once you’ve pulled down the image you want, you can make sure it’s there with the command:

The above command will list out all of the images you’ve pulled (Figure A).

Figure A: Both the NGINX and nginx/nginx-ingress images are available to be used.

Do use caution when pulling down random images. Why? Because you never know what went into creating them. A container could contain malicious code, which could wreak havoc on your network or data. Because of this, it’s always best to only work with the official images (such as the NGINX image).

Deploying the Container

It’s now time to deploy the container. Had you not already pulled down the image, the requested image would be pulled down during the deployment phase. Since we pulled down the official NGINX image, we’ll be using that.

To deploy a container, you use the docker command like so:

Where:

  • NAME equals a name you want to give the container (this could be anything, like nginx-webserver).
  • PORTS the ports you want to use (in the form NETWORK PORT:CONTAINER PORT).
  • IMAGE the image to be used for the container (such as nginx).

So, the very basic command to deploy our NGINX container would be:

The container would be deployed and the NGINX web server would be available to your local network on port 80. What if, however, port 80 was already taken on the server being used to deploy the container? You could deploy it on network port 8080, like so:

At this point, you might see the next issue. After running one of the above commands, the bash prompt isn’t returned (Figure B).

Figure B: The container gives us feedback as the NGINX-powered site is visited, but it doesn’t give us our prompt back.

Detached Mode

How do you run a container, and get your bash prompt back? To do this, you must run the container in detached mode. Before you do that, you have to kill the current container with the keyboard combination [Ctrl]+. That combination will give you back the prompt and kill the container.

To make sure a container isn’t still running, issue the command:

This will list out all containers and their status (Figure C).

If the container were still running, we’d have to kill it before we could deploy another container on the same port (otherwise the ports would conflict, preventing the container from deploying). To kill a running container, you first need the Container ID (a random string of characters). This string is presented when the docker ps -a command is issued. To kill a running container, issue the command:

Where CONTAINER_ID is the ID of the container in question.

You can then delete the container with the command:

Where CONTAINER_ID is the ID of the container in question.

Do note, you don’t have to type out the full Container ID, as the first four characters of the string will suffice.

Now, to deploy the container in detached mode, the command would be:

This time you’ll not only get your prompt back, but Docker will display the Container ID for you (Figure D).

Figure D. Our NGINX container, running in detached mode.

Accessing the Running Container

What if you wanted to work on the running container? Say you want to make changes to NGINX or even start developing the website it will display? To do that, you have to access the container. For this, you need the Container ID. With the ID in hand, issue the command:

Where CONTAINER_ID is the ID of the container.

You should now be in the running container prompt (Figure E), where you can start working on the NGINX server.

Figure E: The NGINX container prompt, ready for work.

To exit the container, simply type the command exit.

Easier Than You Thought

Hopefully, by now, you’re seeing that container deployment isn’t nearly as challenging as you thought. With the help of Docker, you can be rolling out apps and services like a pro … in minutes.

Feature image by Daniela Dimitrova from Pixabay.

A newsletter digest of the week’s most important stories & analyses.

View / Add Comments

Please stay on topic and be respectful of others. Review our Terms of Use.