How to make an image from a Docker container - docker

I'm following a tutorial to wrap a tool in a docker container.
In the linked tutorial page, step 2 describes how to create the container
$ docker run -ti ubuntu
and
root#70235f7726cf:/#
I install a number of libraries/programs
$ apt-get install wget build-essential zlib1g-dev libncurses5-dev
[...]
then
exit
Step 3 describes how the docker container is saved into an image but there is only the procedure to save it in a private repo and not in docker hub.
I did some research and the following is the command to push an image to a Docker repository in the Hub
$ docker push myusr/my-repo:mytoolv1
but since I did not save the image the push does not work.
The tutorial I'm following is missing some steps in between or maybe it is me that is missing some knowledge of Docker.

I think you might have some terms mixed up. You can't push containers to dockerhub, you can only push images.
To create a custom image you need a Dockerfile. Something like this:
FROM ubuntu:18.04
RUN apt update
RUN apt install -y wget build-essential zlib1g-dev libncurses5-dev
...
Then from the same folder build the custom image by running
docker build -t myusr/my-repo:mytoolv1 .
Ath thus point you can push the image to dockerhub using the command you tried:
docker push myusr/my-repo:mytoolv1

What is in your mind and is not correct, is that you think you can push container to your local repo, but in fact you are pushing the image.
I hope you know the difference between an image and a container, if not you can search about.
You can create a file called Dockerfile (with no extension and with this exact name) with the following contents:
FROM ubuntu:20.04
# to set time zone as you may encounter some unexpected stuck when selecting time zone during the build
ENV TZ=Asia/Tehran # search more about it in https://www.php.net/manual/en/timezones.php
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN apt-get update && apt-get install -y wget build-essential zlib1g-dev libncurses5-dev
Now you should build your image:
docker build -t yourrepo/NAME:TAG_VERSION .
example:
docker build -t yourrepo/my_image:1.0.0 .
Now you can push it:
docker push yourrepo/my_image:1.0.0

Related

Installing python3.10 in ubuntu container

I am looking for some help in writing docker file for Ubuntu 18.04 version which installs Python3.10.
Currently it is written in such a way that it gets the default version of the Python3 (i.e. 3.6) along with the ubuntu 18.04.
Here the question is, is there any way that I can get the Python3.10 with Ubuntu 18.04? The requirement is to use either slim or non-slim versions of Python3.10 Bulls eye image from docker hub
you can use ubuntu 18 docker image, then install python 3.10 inside it.
FROM ubuntu:18.04
RUN apt-get -y update && apt -get install software-properties-common /
&& add-apt-repository ppa:deadsnakes/ppa && apt install python3.10
I am able to build the image on ubuntu 18.04 by including python3.10
Step-1: Write a docker file
FROM python:3.10-bullseye
RUN mkdir WORK_REPO
RUN cd WORK_REPO
WORKDIR /WORK_REPO
ADD hi.py .
CMD ["python", "-u", "hi.py"]
Step-2: Build the image
docker build -t image_name .
Step-3: Run the docker image
docker run image_name
Step-4: Connect to the container and check the Python version
I hope this would be helpful for someone who is completely new in writing dockerfile.
Many Thanks,
Suresh.

How to setup dbt using docker containers on Windows 10

After reading Dbt documentation, I've had a hard time to figure out how to install dbt-core (or any other packages i.e. dbt-postgres, dbt-snowflake, etc) on Windows 10.
I have Docker Desktop installed, running a couple of containers already (mostly nodeJS containers, and Kafka). However, it was hard to understand how I would have those new Dbt containers available in my Docker Desktop Console.
I can see docker images were installed properly
$docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
**ghcr.io/dbt-labs/dbt-core 1.2.1 802a0d70aedc 4 weeks ago 538MB**
**ghcr.io/dbt-labs/dbt-bigquery 1.2.latest b7502bcd3b35 2 months ago 559MB**
...
postgres latest f8dd270e5152 7 weeks ago 376MB
dpage/pgadmin4 latest d13c9d7d0193 2 months ago 382MB
wurstmeister/kafka latest a692873757c0 4 months ago 468MB
wurstmeister/zookeeper latest 3f43f72cb283 3 years ago 510MB
Does anyone know how to I them to the Desktop Console?
I'm currently on Windows 10 and use a Docker image for my dbt project without needing WSL. Below is my Dockerfile and requirements.txt file with dbt-core and dbt-snowflake but feel free to swap the packages you need.
In my repo, my dbt project is in a folder at the root level named dbt.
requirements.txt
dbt-core==1.1.0
dbt-snowflake==1.1.0
Dockerfile
FROM public.ecr.aws/docker/library/python:3.8-slim-buster
COPY . /dbt
# Update and install system packages
RUN apt-get update -y && \
apt-get install --no-install-recommends -y -q \
git libpq-dev python-dev && \
apt-get clean && \
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
# Install dbt
RUN pip install -U pip
RUN pip install -r dbt/requirements.txt
# TEMP FIX due to dependency updates. See https://github.com/dbt-labs/dbt-core/issues/4745
RUN pip install --force-reinstall MarkupSafe==2.0.1
# Install dbt dependencies
WORKDIR /dbt
RUN dbt deps
# Specify profiles directory
ENV DBT_PROFILES_DIR=.dbt
# Expose port for dbt docs
EXPOSE 8080
And then you can build and run it (I personally put both of these commands in a dbt_run.sh file and run with bash dbt_run.sh):
docker build -t dbt_image .
docker run \
-p 8080:8080 \
--env-file .env \
-it \
--mount type=bind,source="$(pwd)",target=/dbt \
dbt_image bash
If you make changes to your dbt project while the container is running they will be reflected in the container which makes it great for developing locally. Hope this helps!
I finally was able to pull the image. To add a container in the Docker desktop, I just needed to actually run it.
However, running a dbt-core container in docker, it returns an error:
right after I start the container it stops and returns exit(1), as per the screenshot.

docker image run failing, ipmi_exporter for Prometheus

I'm trying to create a docker image of soundcloud/ipmi-exporter to run with Prometheus on Ubuntu Bionic with Docker 19.03.6, build 369ce74a3c. Docker on my OS X laptop is Docker version 20.10.2, build 2291f61. I am forced to build the (customized) image on my laptop because Bionic has a version of golang that's older than what ipmi-exporter wants, and I'm not allowed to update the Ubuntu server.
Anyway, can someone tell me what I'm doing wrong in my Dockerfile?
# Container image
FROM quay.io/prometheus/golang-builder:1.13-base AS builder
ADD . /go/src/github.com/soundcloud/ipmi_exporter/
RUN cd /go/src/github.com/soundcloud/ipmi_exporter && make
# Container image
FROM ubuntu:18.04
WORKDIR /
RUN apt-get update && apt-get install freeipmi-tools -y --no-install-recommends && rm -rf /var/lib/apt/lists/*
COPY --from=builder /go/src/github.com/soundcloud/ipmi_exporter/ipmi_exporter /bin/ipmi_exporter
EXPOSE 8888
ENTRYPOINT ["ipmi_exporter"]
CMD ["--config.file", "/ipmi_remote.yml"]
CMD ["--web.listen-address=":8889"" "--freeipmi.path=/etc/freeipmi" "--log.level="debug""]
When I run the image all I see is
ipmi_exporter: error: unexpected /bin/sh, try --help
I have ipmi_exporter running on the OS directly and I never configured a config.yml. What config.yml is the Dockerfile author talking about? It's mentioned in the last line of https://github.com/soundcloud/ipmi_exporter/blob/master/Dockerfile
The image lives here: https://github.com/soundcloud/ipmi_exporter The sample/example Dockerfile refers to a config.yaml which this software does not use.
I just can't figure out how to make the image pull in the config file I specify.

Creating first docker container: Can't find host system file on build

I'm trying to bundle my Jekyll blog as a docker container.
I found this Dockerfile which seems to suit my use case but wanted to be more hands on so I copied it directly into my repo:
FROM ruby:latest
MAINTAINER Peter Etelej <peter#etelej.com>
RUN apt-get -qq update && \
apt-get -qq install nodejs -y && \
gem install -q bundler
RUN mkdir -p /etc/jekyll && \
printf 'source "https://rubygems.org"\ngem "github-pages"\ngem "execjs"\ngem "rouge"' > /etc/jekyll/Gemfile && \
printf "\nBuilding required Ruby gems. Please wait..." && \
bundle install --gemfile /etc/jekyll/Gemfile --clean --quiet
RUN apt-get clean && \
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
ENV BUNDLE_GEMFILE /etc/jekyll/Gemfile
EXPOSE 4000
ENTRYPOINT ["bundle", "exec"]
CMD ["jekyll", "serve","--host=0.0.0.0"]
When I run it I get an error
jekyll 3.4.3 | Error: No such file or directory # rb_sysopen - /etc/modules-load.d/modules.conf
The host system has this file but my assumption was that the container didn't have access to it so I tried to add it into the Dockerfile
ADD /etc/modules-load.d/modules.conf /etc/modules-load.d/modules.conf
I then docker build and get the error
lstat etc/modules-load.d/: no such file or directory
I don't understand why the container is looking for this file in the first place but I'm even more confused by the fact that I can't add a file which is clearly there.
Docker builds run on the docker host, not necessarily the client where you run the command, and so all the files needed to run the build are sent in the build context to the host. That context is most often the current directory, or ., that you pass at the end of the docker build -t $image_name . command.
Everything that you try to include in the image with a COPY or ADD is done in reference to that build context, not the filesystem on your client or host machine. So if you need a modules.conf, you'll need to first copy that into your directory with the Dockerfile, and then COPY the file from there.
As for why jekyll is looking for the file, I'm not familiar with jekyll, but it doesn't look promising for something running inside of a container. The modules are kernel specific and containers are designed to be moved to different hosts with potentially different kernels.

Can't figure out how I have to build my Docker architecture

So currently I'm building an API in PHP as different (micro) services which runs on nginx.
I've followed all the Docker fundamental video's and went through the docs, but I still can't figure out how to implement it.
Do I need a server where I push my code to and deploy on the containers (with CI or so)?
Does the container volume get pushed to the hub as well? So my code will be in the container itself?
I think you messed up a bit what's container and what's an image. For me image is something you build on disk to run. And container is an image running on the computer and serving/doing things.
Do I need a server where I push my code to and deploy on the containers
No, you don't. You start building image from some base image, and from a Dockerfile. So make some work dir, copy Dockerfile here, copy your PHP sources here as PHPAPI and in Dockerfile have commands to copy PHP into docker. Along the lines
FROM ubuntu:15.04
MAINTAINER guidsen
RUN apt-get update && \
apt-get install -y nginx && \
apt-get install -y php && \
apt-get autoremove; apt-get clean; apt-get autoclean
RUN mkdir -p /root/PHPAPI
COPY PHPAPI /root/PHPAPI
WORKDIR /root/PHPAPI
CMD /root/PHPAPI/main.php
Does the container volume get pushed to the hub as well? So my code will be in the container itself?
That depends on what do you use to run containers from image. AWS I think require image pulled from Docker hub, so you have to push it here first. Some other cloud providers or private clouds require to push image directly to them. And yes, your code would be in the image and will be run in the container.

Resources