Setting up Ubuntu Development Environment with Docker on MacOS

Shen Lu
Shen Lu
Posted on Aug 09, 2024
views views
2 min read (358 words)

After setting up the Mac terminal, I tried developing the programs I had previously worked on in Ubuntu. However, I encountered an issue where Bun was unable to install packages properly after its installation. I have been stuck on this problem for the entire day and cannot find a solution.

Thus, I gave up to set up development environment on macOS directly and attempted to create a Ubuntu development environment, as same as previous Ubuntu development environment, on my Macbook.

Using Docker to set up an Ubuntu development environment on a Mac has several advantages compared to setting up the environment directly on the Mac:

    1. Consistency: Ensures the development environment is consistent across different systems.
    1. Isolation: Keeps the development environment isolated from the host system, preventing potential conflicts.
    1. Portability: Easily share the environment with others by sharing the Docker image or container.
    1. Reproducibility: Quickly reproduce the environment on any machine with Docker installed.
    1. Ease of Cleanup: Easily remove or reset the development environment without affecting the host system.

Install Docker

Install Docker, drag and drop it in Applications.

Check if Docker could work well.

docker -v
Docker version 27.0.3, build 7d4bcd8

Create Dockerfile for Ubuntu Development Environment

Docker can build images automatically by reading the instructions from a Dockerfile. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.

I make a Dockerfile with the commands to build an images with development environment as same as my previous Ubuntu.

# Use the latest Ubuntu image
FROM ubuntu:latest
 
# Update and install dependencies
RUN apt-get update && \
    apt-get install -y curl git tmux
 
# Install Bun
RUN curl -fsSL https://bun.sh/install | bash

To build a docker image, run the following command:

docker build -t ubuntu_on_mac .
docker images

Then run docker image to create an container.

docker run -itd -p 3000:3000 -v $HOME/ubuntu:/home --name ubuntu ubuntu_on_mac
docker ps

Reference