Run your image

Run your image as a container

A container is a normal operating system process except that this process is isolated in that it has its own file system, its own networking, and its own isolated process tree separate from the host.

To run an image inside of a container, we use the docker run command and to publish a port for our container, we’ll use the --publish flag (-p for short) on the docker run command. The format of the --publish command is [host port]:[container port].

So, if we wanted to expose port 80 inside the container to port 5000 outside the container, we would pass 5000:80 to the --publish flag.

Run the container using the following command:

docker run --publish 5000:80 dotnet-docker

Now, let’s access http://localhost:5000 in a browser. You should see a page similar to the following image.

Success! We were able to connect to the application running inside of our container on port 80.

Press Ctrl-C to stop the container. (or kill the terminal if it does not respond to control C)

Run in detached mode

This is great so far, but our sample application is a web server and we don’t have to be connected to the container. Docker can run your container in detached mode or in the background. To do this, we can use the --detach or -d for short. Docker starts your container the same as before but this time will “detach” from the container and return you to the terminal prompt.

docker run -d -p 5000:80 dotnet-docker

Docker started our container in the background and printed the Container ID on the terminal.

Again, let’s make sure that our container is running properly. In a web browser, access http://localhost:5000

Last updated