Docker
  • Introduction
    • What is Virtualization?
      • What is a Hypervisor?
      • Microsoft Hyper-V
    • What is Containerization?
      • What is docker?
      • What is a container image?
      • When to use containers?
      • When not to use Docker containers
  • Basics
    • ▶️Pull and run an image
      • Trivia
    • Basic docker commands
    • Container life cycle
  • Build an Image
    • ▶️Console app experiment
      • Hello-world image
    • What is a docker file?
      • Our docker file explained
    • ▶️Web app experiment
      • Create application
      • Create a docker file
      • Build an image
      • Run your image
      • Manipulating containers
  • Layers & Stages
    • Introduction
    • Image layers
    • Multistage images
Powered by GitBook
On this page
  • Run your image as a container
  • Run in detached mode
  1. Build an Image
  2. Web app experiment

Run your image

PreviousBuild an imageNextManipulating containers

Last updated 1 year ago

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 in a browser. You should see a page similar to the following image.

Image of app page

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
http://localhost:5000