docker , cookbook ,

Docker: Cookbook

by Abhith Rajan FollowOctober 28, 2018 · 6 min read · Last Updated:
Share this

This article is my own reference for Docker development, contains solutions for problems I have faced while working with Docker and some helpful external resources.

Table of Contents

Problems

1. docker: “build” requires 1 argument. See 'docker build --help'

Most probably you missed a dot, need to add it, example

docker build -t docusaurus-doc .

It means you use the Dockerfile in the local directory

2. “read-only file system” error running Docker Toolbox in Windows 7

Try restarting the docker machine by following below steps:

First, you need to find the name of your docker machine

 docker-machine ls

Then with your machine name, run the command

docker-machine restart <name>

3. Docker for windows - Mapping docker to localhost

You can do it by configuring your Virtualbox. Follow below steps,

  • VirtualBox -> Your BOX -> Settings -> Network ->
  • Choose NAT
  • Open Advanced
  • Click Port Forwarding
  • Add a new rule to map the desired port you need from host to guest
  • Ok/Save
  • Stop Box
  • Start the Box

4. COPY failed: CreateFile

In my visual studio solution, containing a .NET Core Console app, I enabled Docker support using Visual studio container tools on Windows.

 Visual studio container tools on Windows

And it generated a Dockerfile inside the selected project but when i tried to build an image from inside the project directory using the docker command

docker build -t myimagename .

It failed with following exception,

COPY ["ProjectDirectory/ProjectName.csproj", "ProjectDirectory/"] COPY failed: CreateFile \?\C:\ProgramData\Docker\tmp\docker-builder366701720\ProjectDirectory\ProjectName.csproj: The system cannot find the path specified.

This was because I tried to run this from inside the ProjectDirectory. Why I tried to run from there was because the while enabling docker support via Visual Studio 2019 context menu, the Dockerfile generated inside that project directory, not in the root directory of solution.

So inorder to fix this, I moved the same Dockerfile to the root directory of the solution. It can be done via Powershell command

mv Dockerfile ../Dockerfile

Then cd into the root directory of the solution where now the Dockerfile is, and run the build command, and it worked.

5. Force Docker for a clean build (not using cache)

Using --no-cache in your build command.

eg

docker build --no-cache -t myimagename .

6. Rename a Container

Using the docker rename command.

docker rename CONTAINER_ID my_new_container_name

7. $'\r': command not found

This is occurred when I tried to run some sh file in my linux container. That sh file was saved in CRLF end of line sequence, changing it to LF solved the problem.

If you wonder what is End of line sequence,

The End of Line (EOL) sequence ( 0x0D 0x0A , \r\n ) is actually two ASCII characters, a combination of the CR and LF characters. It moves the cursor both down to the next line and to the beginning of that line.

If you are using Vscode, switching EOL is easy, on the bottom right corner, you can find the current EOL sequence of the editing file, by clicking on it, Vscode will present you a dropdown to choose from. Select the LF in this case, and save file.

8. Copy docker image from one host another

By using docker save command, we can export docker image as a .tar file.

docker save -o /path/image_name.tar image_name

The above will create an image_name.tar file in the path specified. Copy it to the new host, then you can import the image to the new host by using docker load like below,

docker load -i <path to image tar file>

9. Dotnet core build error when building docker image - Missing Newtonsoft.Json

In my case, When used JsonProperty in a class,

Visual Studio intellicode auto-filled,

using Newtonsoft.Json;

Then during docker build,

warning MSB3245: Could not resolve this reference. Could not locate the assembly "Newtonsoft.Json". Check to make sure the assembly exists on disk.

When I checked, found out that the Visual Studio added an Assembly Reference to Newtonsoft.Json (you can find this by expanding Dependencies node in the solution explorer in Visual Studio). And I was using Linux images.

So in order to solve this, I removed the Assembly Reference, and added nuget package Newtonsoft.Json, then the docker build was successful.

Questions

1. Difference between docker run and start

In short,

  • run : runs an image. i.e it will create a container from that image first and then starts the container. All in one command.
  • start : starts the specified container.

So if you created a container from an image using the create command, then you can use start command to start that container. Or you can start a container from the image by using the run command.

CLI

docker-compose

Build the images, do not start the containers
docker-compose build
Build the images if the images do not exist and start the containers
docker-compose up
Force docker-compose to build from scratch, no cache
docker-compose build --no-cache
Force to build the images even when not needed
docker-compose up --build
Run multiple containers of the same image
docker-compose up --scale <SERVICE_NAME>=n

Replace the <SERVICE_NAME> with the appropriate service listed in the docker-compose.yml and n with the number of containers you want, in order to create that number of containers of that service.

Build single container using docker-compose
docker-compose build <SERVICE_NAME>

Replace the <SERVICE_NAME> with the appropriate service listed in the docker-compose.yml.

External Resources

This page is open source. Noticed a typo? Or something unclear?
Improve this page on GitHub

Abhith Rajan

Written by Abhith Rajan FollowBuy me a coffee
Abhith Rajan is an aspiring software engineer with more than 6 years of experience and proven successful track record of delivering technology-based products and services.