Is it possible to run Ubuntu server on Docker?

6,330

The first mistake is to think that a Docker image/container is similar to a virtual machine. There are resemblances but they aren't the same and frankly, not really similar.

Docker is not, really, for running an OS. It is for running a single process in a contained (isolated) environment. The container uses the same kernel as what is on the host.

When you create a Docker image with FROM ubuntu you are starting your image with some pre-created layers that brings in some parts of a standard file system and packages you would find on a Ubuntu server.

Then you add your own additional layers, adding binaries and files that are necessary to run your program / process.

The image would (normally but not mandatory) have a CMD command or ENTRYPOINT command to run something.

Each line in a Dockerfile is a command instructing Docker on how to create the image. Each line/command result in one more layer. To install packages, you probably want to do something like this:

FROM ubuntu:16.04
RUN apt-get -qq update && \
    apt-get -y install build-essential autoconf libtool && \
    apt-get install -y python-setuptools python-dev python3-dev && \
    apt-get install -y python-pip python3-pip && \
    apt-get install -y python-virtualenv unixodbc-dev libffi-dev git && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

That will keep your image smaller.

Then you will need to actually run something...

CMD python

Then you can build your image:

docker build -t myimage .

And use it...

docker run --rm -it myimage

ifconfig does not work because the binary is not in the image. It is not really necessary as you don't manage the network from within the container.

So you seem to be making a container to build some code. You would want to mount your directory with the code inside the container when you run it. I don't know what OS you run on your workstation or server so I will refer to this post for more information: https://stackoverflow.com/questions/41485217/mount-current-directory-as-a-volume-in-docker-on-windows-10/41489151#41489151

Let's say you are building with the command make, you could do this:

docker run --rm -it -v $(pwd):/usr/src/project myimage make

This would require a line in your Dockerfile to make the working dir /usr/src/project:

WORKDIR /usr/src/project

What will happen if you ran the docker command line above is that it will create a container from the image named myimage (build command shown earlier), mount the current directory you are in as /usr/src/project inside the new container. Run the make command inside the container and then exit. The --rm parameter tells Docker to not keep the container around once it finish running. The -it parameters mean interactive and tty.

If you wanted to just have access to some shell so you could manually run make or other commands in a ad-hoc fashion, you could also do this:

docker run --rm -it -v $(pwd):/usr/src/project myimage /bin/bash

This would create the container from the myimage image, and run bash. Because bash does not exit and you have the -it parameter, you will be at the container's prompt where you can do whatever you want.

Just remember that any file you modify from inside the container will not be preserved and next time you start back to default state. Of course any files from the /usr/src/project directory will be modified on your local hard drive as this is mounted from it.

Share:
6,330

Related videos on Youtube

Jay
Author by

Jay

Updated on September 18, 2022

Comments

  • Jay
    Jay over 1 year

    My team has been developing our project on Ubuntu 16.04 server running on VirtualBox. As new developers come join our project, it is a big hassle for us help them create a new Ubuntu server on VirtualBox and install all the dependencies just to start the project every time.

    We decided to solve the hassle with Docker. So, I'm trying to run our project on Ubuntu on Docker with the below Dockerfile and docker-compose.yml, but I can't find many resources like how to run Ubuntu on Docker. Am I going to a right direction for running Ubuntu on Docker with the below way?

    One thing I'm confused with is why I can't get ip address when I run ifconfig on my Ubuntu server running on Docker?

    Dockerfile

    FROM ubuntu:16.04
    
    RUN sudo apt-get update
    RUN sudo apt-get install -y build-essential autoconf libtool 
    RUN sudo apt-get install -y python-setuptools python-dev python3-dev
    RUN sudo apt-get install -y python-pip python3-pip
    RUN sudo apt-get install -y python-virtualenv unixodbc-dev libffi-dev git
    ...
    

    docker-compose.yml

    version: '3'
    
    services:
      ubuntu:
        build:
          context: .
          dockerfile: ./Dockerfile
        ports:
         - "8000:8000"
        container_name: dev_ubuntu
    ...