1
0
Fork 0
mirror of https://github.com/NixOS/nix.dev.git synced 2024-10-18 14:32:43 -04:00

Merge pull request #326 from worktheclock/patch-9

Building and running Docker images RITZA edit
This commit is contained in:
Domen Kožar 2022-09-26 13:38:51 +02:00 committed by GitHub
commit 22ae50fb76
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -7,25 +7,20 @@ myst:
# Building and running Docker images # Building and running Docker images
[Docker](https://www.docker.com/) is a set of tools and services used to [Docker](https://www.docker.com/) is a set of tools and services used to build, manage and deploy containers.
build, manage and deploy containers.
As many cloud platforms offer Docker-based As many cloud platforms offer Docker-based container hosting services, creating Docker containers for a given service is a common task when building reproducible software.
container hosting services, creating Docker containers for a given service is a In this tutorial, you will learn how to build Docker containers using Nix.
common task when building reproducible software. In this tutorial, you will
learn how to build Docker containers using Nix.
## Prerequisites ## Prerequisites
We assume you have both Nix and [Docker installed](https://docs.docker.com/get-docker/). Docker is available in You will need both Nix and [Docker](https://docs.docker.com/get-docker/) installed.
`nixpkgs`, which is the preferred way to install it on NixOS. However, you can Docker is available in `nixpkgs`, which is the preferred way to install it on NixOS.
also use the native Docker installation of your OS, if you are on another Linux However, you can also use the native Docker installation of your OS, if you are on another Linux distribution or macOS.
distribution or MacOS.
## Build your first container ## Build your first container
[Nixpkgs](https://github.com/NixOS/nixpkgs) provides `dockerTools` to create [Nixpkgs](https://github.com/NixOS/nixpkgs) provides `dockerTools` to create Docker images:
Docker images:
```nix ```nix
{ pkgs ? import <nixpkgs> { } { pkgs ? import <nixpkgs> { }
@ -78,38 +73,34 @@ Finished.
/nix/store/y74sb4nrhxr975xs7h83izgm8z75x5fc-docker-image-hello-docker.tar.gz /nix/store/y74sb4nrhxr975xs7h83izgm8z75x5fc-docker-image-hello-docker.tar.gz
``` ```
The image tag (`y74sb4nrhxr975xs7h83izgm8z75x5fc`) refers to the Nix build hash The image tag (`y74sb4nrhxr975xs7h83izgm8z75x5fc`) refers to the Nix build hash and makes sure that the Docker image corresponds to our Nix build.
and makes sure that the Docker image corresponds to our Nix build. The store The store path in the last line of the output references the Docker image.
path in the last line of the output references the Docker image.
## Run the container ## Run the container
To work with the container, load this image into To work with the container, load this image into Docker's image registry from the default `result` symlink created by `nix-build`:
Docker's image registry from the default `result` symlink created by nix-build:
```shell-session ```shell-session
$ docker load < result $ docker load < result
Loaded image: hello-docker:y74sb4nrhxr975xs7h83izgm8z75x5fc Loaded image: hello-docker:y74sb4nrhxr975xs7h83izgm8z75x5fc
``` ```
You can also use the store path to load the image in order to avoid depending on the presence of You can also use the store path to load the image in order to avoid depending on the presence of `result`:
`result`
```shell-session ```shell-session
$ docker load < /nix/store/y74sb4nrhxr975xs7h83izgm8z75x5fc-docker-image-hello-docker.tar.gz $ docker load < /nix/store/y74sb4nrhxr975xs7h83izgm8z75x5fc-docker-image-hello-docker.tar.gz
Loaded image: hello-docker:y74sb4nrhxr975xs7h83izgm8z75x5fc Loaded image: hello-docker:y74sb4nrhxr975xs7h83izgm8z75x5fc
``` ```
Even more conveniently, you can do everything in one command. The advantage of this approach Even more conveniently, you can do everything in one command.
is that `nix-build` will rebuild the image if there are any changes and pass the new store The advantage of this approach is that `nix-build` will rebuild the image if there are any changes and pass the new store path to `docker load`:
path to `docker load`:
```shell-session ```shell-session
$ docker load < $(nix-build hello-docker.nix) $ docker load < $(nix-build hello-docker.nix)
Loaded image: hello-docker:y74sb4nrhxr975xs7h83izgm8z75x5fc Loaded image: hello-docker:y74sb4nrhxr975xs7h83izgm8z75x5fc
``` ```
Now that you have loaded the image into Docker, it is time to run it: Now that you have loaded the image into Docker, you can run it:
```shell-session ```shell-session
$ docker run -t hello-docker:y74sb4nrhxr975xs7h83izgm8z75x5fc $ docker run -t hello-docker:y74sb4nrhxr975xs7h83izgm8z75x5fc
@ -118,20 +109,16 @@ Hello, world!
## Working with Docker images ## Working with Docker images
A general introduction to working with Docker images is not part of this A general introduction to working with Docker images is not part of this tutorial.
tutorial. The [official Docker documentation](https://docs.docker.com/) is a The [official Docker documentation](https://docs.docker.com/) is a much better place for that.
much better place for that. Note that when you build your
Docker images with Nix, you will probably not write a `Dockerfile`
as Nix replaces the Dockerfile functionality within the Docker ecosystem.
Nonetheless, understanding the anatomy of a Dockerfile may still be useful to Note that when you build your Docker images with Nix, you will probably not write a `Dockerfile` as Nix replaces the Dockerfile functionality within the Docker ecosystem.
follow along how Nix replaces each of its functions. Using the Docker CLI, Nonetheless, understanding the anatomy of a Dockerfile may still be useful to understand how Nix replaces each of its functions.
Docker Compose, Docker Swarm or Docker Hub on the other hand may still be Using the Docker CLI, Docker Compose, Docker Swarm or Docker Hub on the other hand may still be relevant, depending on your use case.
relevant depending on your use case.
## Next steps ## Next steps
- More details on how to use `dockerTools` can be found in the [reference documentation](https://nixos.org/nixpkgs/manual/#sec-pkgs-dockerTools). - More details on how to use `dockerTools` can be found in the [reference documentation](https://nixos.org/nixpkgs/manual/#sec-pkgs-dockerTools).
- You will also want to [browse through more examples of Docker images built with Nix](https://github.com/NixOS/nixpkgs/blob/master/pkgs/build-support/docker/examples.nix). - You might like to browse through more [examples of Docker images built with Nix](https://github.com/NixOS/nixpkgs/blob/master/pkgs/build-support/docker/examples.nix).
- [Arion](https://docs.hercules-ci.com/arion/), docker-compose wrapper with first-class support for Nix. - Take a look at [Arion](https://docs.hercules-ci.com/arion/), a `docker-compose` wrapper with first-class support for Nix.
- Build docker images on a {ref}`CI with Github Actions <github-actions>` - Build docker images on a {ref}`CI with Github Actions <github-actions>`.