Bill Blogs in C# -- .NET Core

Bill Blogs in C# -- .NET Core

Created: 10/6/2016 6:24:06 PM

I’ve been working with Docker on both Windows and the Mac these past few weeks. Everything I’ve been doing is command line based. In this post, I list the commands I use most often, with the options I need the most.

Disclaimer: This not meant to be a complete Docker reference. It’s a quick way to remember the commands and options I use most often. Your mileage may vary. All of these topics are covered in more depth on the Docker site. For specific .NET Content, checkout the .NET Core and .NET Framework content for running .NET applications in Docker.

As you start working with Docker, you must be able to distinguish between Docker Images and Docker Containers. A great way to explain it is for C# developers is that images correspond to classes and containers correspond to objects. An image provides a template for containers. A container is a running copy of an image.

Creating and Managing Images

You build images with “docker build”. That command reads a “Dockerfile” that describes the image you want built. Typcially, I use:

docker build –t <ImageName> .

This command builds your image. The ‘-t’ argument lets you tag the image. The ‘.’ is a required argument that specifies the directory for the Dockerfile.

You look at yoru catalog of images using:

docker images

This command lists the images and their tags.

When you no longer need an image, your use:

docker rmi <ImageName>

The <ImageName> is the name you gave to this image when you built it.

Starting and Stopping Containers

You launch a container using docker run. I usually specify these arguments:

docker run –d –p 80:8000 –name <ContainerName> <ImageName>

‘-d’ means your container runs in detached mode, in the background. The ‘-p’ specifies port mapping. The first port is the port used on the host, and second is the port used on the container. In the example above, your application would be listening on port 8000 in the containers, and external processes would make a request on port 80. Docker maps those requests.

I use the – --name argument in development environments to give a name to my containers. Docker assigns each container an id (basically a sha) and you must use that to manage a container unless you have given it a name.

That makes it easier for me to manage them. The last argument is the image name to start.

To see your running containers, use:

docker ps

By default, this only shows the running images. IF you want to see all images, use the ‘-a’ argument.

To stop the container, use:

docker stop <ContainerNameOrSHA>

Stopping doesn’t remove a container. (You can still see it with ‘docker ps –a’). If you want to restart a container that you have stopped, use:

docker start <ContainerNameOrSHA>

To remove an image that you have stopped, use:

docker rm <ContainerNameOrSHA>

When you remove an image, you can reuse the name specified above in docker run.

Those are the commands and options I use most often when I’m working with Docker. Hope that helps.

Current Projects

I create content for .NET Core. My work appears in the .NET Core documentation site. I'm primarily responsible for the section that will help you learn C#.

All of these projects are Open Source (using the Creative Commons license for content, and the MIT license for code). If you would like to contribute, visit our GitHub Repository. Or, if you have questions, comments, or ideas for improvement, please create an issue for us.

I'm also the president of Humanitarian Toolbox. We build Open Source software that supports Humanitarian Disaster Relief efforts. We'd appreciate any help you can give to our projects. Look at our GitHub home page to see a list of our current projects. See what interests you, and dive in.

Or, if you have a group of volunteers, talk to us about hosting a codeathon event.